package es.ats.codington.festival.junit.services;

import static org.junit.Assert.*;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.*;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.junit.utils.ScriptRunner;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.exceptions.ServiceException;

public class TEventService {
	private static EventService es;
 	private Event ev;
 	private static int badEventId=10000;	 
		    @BeforeClass
		    public static void onceExecutedBeforeAll() {
		    	es=new EventService();
				ScriptRunner sr=new ScriptRunner(es.getEdi().getCon().connect(),false,true);
				try {
					FileReader fr=new FileReader("sql/newcodingtonCDTdb.sql");
					sr.runScript(fr);
				} catch (Exception e) {
					e.printStackTrace();
				}
				es.getEdi().getCon().disconnect();
		    }
		 
		    @Before
		    public void executedBeforeEach() {
				//24 Dec 2015 21:30:00 GMT
				Date d= new Date(1450992600);
				//placeId, placeName, capacity, placeDescription, workingHours
				Place p=new Place(1,"Name of the place", 1000,"Description of the place",555, 0);
				//name, place, eventId, description, type, price, duration, seatsAvailable, start
				this.ev=new Event("Name of the event", p, 1, "Description of the event","Type of the event", 999.95F, 300, 1000, d, 0);
		    }
		    
		    @Test
			/**
			 * Positive testcase for insertEvent 
			 */
			public void testInsertEvent_Positive() {
		    	int a=es.insertEvent(this.ev);
				assertEquals(a, 1);
			}
		    
		    @Test
			/**
			 * Positive testcase for getEventById 
			 */
			public void testGetEventById_Positive() {
		    	Event ev2=es.getEventById(ev.getEventId());
				assertEquals(ev.getEventId(), ev2.getEventId());
			}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for getEventById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testGetEventById_Negative() {
			    es.getEventById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for checkEventSeatsAvailableById 
			 */
		    public void testCheckEventSeatsAvailableById_Positive() {
		    	int s=es.checkEventSeatsAvailableById(ev.getEventId());
				assertEquals(ev.getSeatsAvailable(), s);
			}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventSeatsAvailableById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventSeatsAvailableById_Negative() {
			    es.checkEventSeatsAvailableById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for checkEventNameById 
			 */
		    public void testCheckEventNameById_Positive() {
		    	String s=es.checkEventNameById(ev.getEventId());
				assertEquals(ev.getName(), s);
			}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventNameById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventNameById_Negative() {
			    es.checkEventNameById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for checkEventDescriptionById 
			 */
		    public void testCheckEventDescriptionById_Positive() {
		    	String s=es.checkEventDescriptionById(ev.getEventId());
				assertEquals(ev.getDescription(), s);
			}
			
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventDescriptionById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventDescriptionById_Negative() {
			    es.checkEventDescriptionById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for checkEventStartById 
			 */
		    public void testCheckEventStartById_Positive() {
		    	Date s=es.checkEventStartById(ev.getEventId());
				assertEquals(s, s);
			}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventStartById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventStartById_Negative() {
			    es.checkEventStartById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for checkEventDurationById 
			 */
		    public void testCheckEventDurationById_Positive() {
		    	int s=es.checkEventDurationById(ev.getEventId());
				assertEquals(ev.getDuration(), s);
			}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventDurationById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventDurationById_Negative() {
			    es.checkEventDurationById(badEventId);
			}
		    
		    @Test
		   	/**
		   	 * Positive testcase for checkEventPriceById 
		   	 */
		       public void testCheckEventPriceById_Positive() {
		       	float s=es.checkEventPriceById(ev.getEventId());
		   		assertEquals(ev.getPrice(), s, 0.001);
		   	}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventDurationById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventPriceById_Negative() {
			    es.checkEventPriceById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for checkEventPlaceById 
			 */
		    public void testcheckEventPlaceById_Positive() {
		    	int s=es.checkEventPlaceById(ev.getEventId());
				assertEquals(ev.getPlace().getPlaceId(), s);
			}
		    
		    @Test(expected=ServiceException.class)
		    /**
			 * Negative testcase for checkEventPlaceById 
			 * if an event doesn't exist, must trhow an Exception
			 */
			public void testCheckEventPlaceById_Negative() {
			    es.checkEventPlaceById(badEventId);
			}
		    
		    @Test
			/**
			 * Positive testcase for updateEventNameById 
			 */
		    public void testUpdateEventNameById_Positive(){
		    	String name="Testing new name";
				int s=es.updateEventNameById(ev.getEventId(),name);
				String new_name=es.checkEventNameById(ev.getEventId());
				assertEquals(1, s);
				assertEquals(name, new_name);
			}
		  
		    @Test
			/**
			 * Negative testcase for updateEventNameById
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testUpdateEventNameById_Negative(){
		    	String name="Testing new name";
				int s=es.updateEventNameById(badEventId,name);
				assertEquals(0, s);
			}
		    
		    @Test
			/**
			 * Positive testcase for updateEventDescriptionById 
			 */
		    public void testUpdateEventDescriptionById_Positive(){
		    	String des="Testing new description";
				int s=es.updateEventDescriptionById(ev.getEventId(),des);
				String new_des=es.checkEventDescriptionById(ev.getEventId());
				assertEquals(1, s);
				assertEquals(des, new_des);
			}
		    
		    @Test
			/**
			 * Negative testcase for updateEventDescriptionById 
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testUpdateEventDescriptionById_Negative(){
		    	String name="Testing new name";
				int s=es.updateEventDescriptionById(badEventId,name);
				assertEquals(0, s);
			}

		    @Test
			/**
			 * Positive testcase for updateEventStartById 
			 */
		    public void testUpdateEventStartByIdById_Positive(){
		    	Date d=new Date(1231222312);
				int s=es.updateEventStartById(ev.getEventId(),d);
				//Date new_d=es.checkEventStartById(ev.getEventId());
				assertEquals(1, s);
				assertEquals(1, 1);
			}
		    
		    @Test
			/**
			 * Negative testcase for updateEventStartById 
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testUpdateEventStartById_Negative(){
		    	Date d=new Date(1231222312);
				int s=es.updateEventStartById(badEventId,d);
				assertEquals(0, s);
			}

		    @Test
			/**
			 * Positive testcase for updateEventDurationById 
			 */
		    public void testUpdateEventDurationByIdById_Positive(){
		    	int d=1234;
				int s=es.updateEventDurationById(ev.getEventId(),d);
				int new_d=es.checkEventDurationById(ev.getEventId());
				assertEquals(1, s);
				assertEquals(d, new_d);
			}
		    
		    @Test
			/**
			 * Negative testcase for updateEventDurationById 
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testUpdateEventDurationById_Negative(){
		    	int d=1234;
				int s=es.updateEventDurationById(badEventId,d);
				assertEquals(0, s);
			}

		    @Test
			/**
			 * Positive testcase for updateEventPriceById 
			 */
		    public void testUpdateEventPriceByIdById_Positive(){
		    	float d=1234.56F;
				int s=es.updateEventPriceById(ev.getEventId(),d);
				float new_d=es.checkEventPriceById(ev.getEventId());
				assertEquals(1, s);
				assertEquals(d, new_d, 0.001);
			}
		    
		    @Test
			/**
			 * Negative testcase for updateEventPriceById 
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testUpdateEventPriceById_Negative(){
		    	float d=1234.56F;
				int s=es.updateEventPriceById(badEventId,d);
				assertEquals(0, s);
			}

		    @Test
			/**
			 * Positive testcase for updateEventPlaceById 
			 */
		    public void testUpdateEventPlaceByIdById_Positive(){
		    	int d=2;
				int s=es.updateEventPlaceById(ev.getEventId(),d);
				int new_d=es.checkEventPlaceById(ev.getEventId());
				assertEquals(1, s);
				assertEquals(d, new_d);
			}
		    
		    @Test
			/**
			 * Negative testcase for updateEventPlaceById 
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testUpdateEventPlaceById_Negative(){
		    	int d=2;
				int s=es.updateEventPlaceById(badEventId,d);
				assertEquals(0, s);
			}
		    
		    @Test
			/**
			 * Positive testcase for getAllEvents
			 */
		    public void testGetAllEvents_Positive(){
		    	List<Event> events = new ArrayList<Event>();
				events=es.getAllEvents();
				assertEquals(17,events.size());
			}
		    
		    @Test
			/**
			 * Positive testcase for deleteEventById
			 */
			public void testDeleteEventById_Positive() {
		    	int a=es.deleteEventById(ev.getEventId());
				assertEquals(a, 1);
			}
		    
		    @Test
			/**
			 * Negative testcase for deleteEventById 
			 * if an event doesn't exist, must trhow an Exception
			 */
		    public void testDeleteEventById_Negative(){
				int s=es.deleteEventById(badEventId);
				assertEquals(0, s);
			}
		    
}

